﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using Jot;
using Jot.Configuration;
using Jot.Storage;

namespace TestWinForms
{
    public partial class Form1 : Form, ITrackingAware
    {
        #region 是否模态窗体
        private bool isModalForm = false;

        /// <summary>
        /// 是否模态窗体
        /// </summary>
        [Description("是否模态窗体")]
        public bool IsModalForm
        {
            get { return this.isModalForm; }
            set { this.isModalForm = value; }
        }
        #endregion

        public Form1()
        {
            InitializeComponent();

            dataGridView1.DataSource = new List<Person>()
            {
                new Person { Name = "Joe", LastName="Smith", Age = 34 },
                new Person { Name = "Misha", LastName="Anderson", Age = 45 },
            };
            // NOTE: 
            // We cannot call Track(this) in the constructor. Winfors overwrites Top/Left 
            // properties after the constructor, so we must set them in OnLoad instead.
        }

        protected override void OnLoad(EventArgs e)
        {
            if (this.isModalForm)
            {
                // configure tracking for all Form objects
                Services.Tracker
                    .Configure<Form>()
                    // use different id for different screen configurations
                    .Id(f => f.Name, SystemInformation.VirtualScreen.Size)
                    .Properties(f => new { f.Top, f.Width, f.Height, f.Left, f.WindowState })
                    .PersistOn(nameof(Form.Move), nameof(Form.Resize), nameof(Form.FormClosing))
                    // do not track form size and location when minimized/maximized
                    .WhenPersistingProperty((f, p) => p.Cancel = (f.WindowState != FormWindowState.Normal && (p.Property == "Height" || p.Property == "Width" || p.Property == "Top" || p.Property == "Left")))
                    // a form should not be persisted after it is closed since properties will be empty
                    .StopTrackingOn(nameof(Form.FormClosing));

                // track this form
                Services.Tracker.Track(this);
            }

            // track color pickers as separate objects
            // ColorPicker also implements ITrackingAware so no configuration is needed here
            Services.Tracker.Track(colorPicker1);
            Services.Tracker.Track(colorPicker2);
        }

        public void ConfigureTracking(TrackingConfiguration configuration)
        {
            var cfg = configuration.AsGeneric<Form1>()
                // use different id for different screen configurations
                .Id(f => f.Name, SystemInformation.VirtualScreen.Size)
                // a form should not be persisted after it is closed since properties will be empty
                .StopTrackingOn(nameof(Form.FormClosing)); ;

            // include selected tab index when tracking this form
            cfg.Property(f => tabControl1.SelectedIndex);

            // include data grid column widths when tracking this form
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                var idx = i; // capture i into a variable (cannot use i directly since it changes in each iteration)
                cfg.Property(f => dataGridView1.Columns[idx].Width, "grid_column_" + dataGridView1.Columns[idx].Name);
            }
            cfg.PersistOn("Click", this.button2);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process.Start("explorer.exe", (Services.Tracker.Store as JsonFileStore).FolderPath);
        }
    }
}
